package com.ruoyi.hr.controller.hr.welfare.welfareregion;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.hr.domain.foreignKey.EcdCity;
import com.ruoyi.hr.domain.welfare.welfareregion.WelfareRegionConfig;
import com.ruoyi.hr.service.foreignkey.EcdCityService;
import com.ruoyi.hr.service.welfare.welfareRegion.WelfareRegionConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 福利地区配置
 * @date 2019/12/18 8:30
 */
@Controller
@RequestMapping("/welfare/region/welfareRegionConfig")
public class WelfareRegionConfigController extends BaseController {
    @Autowired
    WelfareRegionConfigService welfareRegionConfigService;

    @Autowired
    EcdCityService ecdCityService;

    private String prefix = "hr/welfare/welfareregion/welfareRegionConfig";

    @GetMapping()
    public String welfareRegionConfig() {
        return prefix + "/welfareRegionConfig";
    }

    /**
     * 获取分页数据以及数据总量
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, String> map) {
        startPage();
        List<WelfareRegionConfig> list = welfareRegionConfigService.findAll(map);
        return getDataTable(list);
    }

    /**
     * 启动停用福利地区
     *
     * @param welfareRegionConfig
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(WelfareRegionConfig welfareRegionConfig) {
        return toAjax(welfareRegionConfigService.updateIsDisabled(welfareRegionConfig));
    }

    /**
     * 修改是否默认
     *
     * @param welfareRegionConfig
     */
    @PostMapping("/changeDefault")
    @ResponseBody
    public AjaxResult changeDefault(WelfareRegionConfig welfareRegionConfig) {
        return toAjax(welfareRegionConfigService.updateDefault(welfareRegionConfig));
    }

    /**
     * 新增跳转
     *
     * @param mmap
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<EcdCity> ecdCities = ecdCityService.findAll();
        mmap.put("ecdCities", ecdCities);
        return prefix + "/add";
    }

    /**
     * 新增操作
     * @param welfareRegionConfig
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated WelfareRegionConfig welfareRegionConfig) {
        return toAjax(welfareRegionConfigService.insertWelfareRegionConfig(welfareRegionConfig));
    }

    /**
     * 修改跳转
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("edit/{id}")
    public String edit(@PathVariable("id")int id, ModelMap mmap)
    {
        List<EcdCity> ecdCities = ecdCityService.findAll();
        mmap.put("ecdCities", ecdCities);
        mmap.put("welfareRegionConfig", welfareRegionConfigService.findWelfareRegionConfigById(id));
        return prefix + "/edit";
    }

    /**
     * 修改操作
     * @param welfareRegionConfig
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated WelfareRegionConfig welfareRegionConfig) {
        return toAjax(welfareRegionConfigService.updateWelfareRegionConfig(welfareRegionConfig));
    }

    /**
     * 删除操作
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult edit(@Validated String ids) {
        return toAjax(welfareRegionConfigService.deleteWelfareRegionConfig(ids));
    }

    @PostMapping("/findAll")
    @ResponseBody
    public List<WelfareRegionConfig> findAll(){
        return welfareRegionConfigService.findAlla();
    }
}

